package util;

import java.sql.*;

/**
 * @author Mark
 * @date 2020/5/26 23:12
 * @description JdbcUtil
 */
public class JdbcUtil {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://localhost:3306/db0?useSSL=false&serverTimezone=UTC";
        String userName = "root";
        String password = "1234";
        String driver = "com.mysql.cj.jdbc.Driver";
        String tableName = "student";
        // 加载驱动
        Class.forName(driver);
        // 连接数据库
        Connection connection = DriverManager.getConnection(url, userName, password);
        // 创造执行sql条件
        Statement statement = connection.createStatement();
        // 编写sql
        String sql = "select * from " + tableName;
        // 执行sql
        ResultSet resultSet = statement.executeQuery(sql);
        // 输出结果集
        while (resultSet.next()) {
            System.out.println("s_id" + resultSet.getObject("s_id"));
            System.out.println("s_name" + resultSet.getObject("s_name"));
            System.out.println("s_birth" + resultSet.getObject("s_birth"));
            System.out.println("s_sex" + resultSet.getObject("s_sex"));
        }
        // 关闭连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
